package ihs;

import interactionhistoryscrollbareclipse.preferences.PreferenceConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class IHSPlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "interaction-history-scrollbar-eclipse"; //$NON-NLS-1$

	public static final String MARKER_ID = PLUGIN_ID + ".ISMarker";

	public static final String MARKERS[] = { MARKER_ID + "1", MARKER_ID + "2",
			MARKER_ID + "3", MARKER_ID + "4", MARKER_ID + "5", MARKER_ID + "6",
			MARKER_ID + "7", MARKER_ID + "8", MARKER_ID + "9", MARKER_ID + "10" };

	// The shared instance
	private static IHSPlugin plugin;

	private IHSTextEditor editor;

	private HashMap<String, List<Marker>> markers;

	/**
	 * The constructor
	 */
	public IHSPlugin() {
		markers = new HashMap<String, List<Marker>>();
		editor = new IHSTextEditor();
	}

	public List<Marker> addMarker(String aFileName, Marker marker,
			int lengthOfFile) {
		List<Marker> temp = getMarkersForFile(aFileName);
		if (temp.size() == 10) {
			temp.remove(0);
		}

		int region = getRegion(lengthOfFile);
		int x = 0;
		synchronized (temp) {
			for (Marker a : temp) {
				if (a.lineNumber >= marker.lineNumber - region
						&& a.lineNumber <= marker.lineNumber + region) {
					temp.remove(x);
					if (x >= temp.size()) {
						break;
					}
				}
				x++;
			}
		}

		temp.add(marker);

		markers.put(aFileName, temp);
		return temp;
	}

	private int getRegion(int aFileSize) {
		boolean setCustom = getPluginPreferences().getBoolean(
				PreferenceConstants.P_BOOLEAN);
		if (setCustom) {
			return Integer.valueOf(this.getPluginPreferences().getString(
					PreferenceConstants.P_REGIONSIZE));
		} else {
			return getAutomaticRegion(aFileSize);
		}
	}

	private int getAutomaticRegion(int fileSize) {
		int automatic;
		if (fileSize < 100) {
			automatic = 2;
		} else if (fileSize < 250) {
			automatic = 3;
		} else if (fileSize < 500) {
			automatic = 4;
		} else if (fileSize < 1000) {
			automatic = 5;
		} else {
			automatic = 6;
		}
		return automatic;

	}

	public void clearMarkersFromFile(String aFileName) {
		markers.remove(aFileName);
	}

	public List<Marker> getMarkersForFile(String aFile) {

		return (markers.get(aFile) == null) ? new ArrayList<Marker>() : markers
				.get(aFile);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static IHSPlugin getDefault() {
		return plugin;
	}

}
